package handler

import (
	"fmt"
	"gin-admin/model"
	"gin-admin/param"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

//AddRole 添加角色
//@Tags 角色管理
//@summary 添加角色
//@param object body param.RoleParam false "角色实体"
//@Success 200 {object} model.JSONResult "ok"
//@Router /role/add [post]
func AddRole(context *gin.Context) {
	var role param.RoleParam
	error := context.BindJSON(&role)
	if error != nil {
		fmt.Println("error", error)
	}
	fmt.Println(role)
	context.JSON(http.StatusOK, model.JSONResult{
		Code: 10000,
		Msg:  "添加成功",
	})
}

//DeleteRole 删除角色
//@Tags 角色管理
//@Summary 删除角色
//@param id path int true "角色id"
//@Success 200 {object} model.JSONResult "ok"
//@Router /role/del/{id} [delete]
func DeleteRole(ctx *gin.Context) {
	var id = ctx.Param("id")
	i, err := strconv.Atoi(id)
	var rtn model.JSONResult
	if err != nil {
		rtn.Code = 10001
		rtn.Msg = "参数格式错误"
	} else {
		rtn.Code = 10000
		rtn.Msg = id
	}
	fmt.Println(i)
	ctx.JSON(http.StatusOK, rtn)
}

//UpdateRole 更新角色
//@Tags 角色管理
//@Summary 更新角色
//@Param id path int true "角色id"
//@Param name body string true "角色名称"
//@Param description body string true "角色名称"
//@Param Authorization header string true "Authorization验证"
//@Success 200 {object} model.JSONResult
//@Router /role/update/{id} [put]
func UpdateRole(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.Atoi(id)
	var rtn model.JSONResult
	if err != nil {
		rtn.Code = 10001
		rtn.Msg = "参数格式化错误"
	} else {
		rtn.Code = 10000
		rtn.Msg = id
	}
	fmt.Println(i)
	ctx.JSON(http.StatusOK, rtn)
}

//GetRoles 获取角色列表
//@Tags 角色管理
//@Summary 角色列表
//@Accept json
//@Produce json
//@Param object query param.RoleQuery true "当前页数" default(1)
//@Success 200 {object} model.JSONResult
//@Router /role/list [get]
func GetRoles(ctx *gin.Context) {
	context := ctx.ContentType()
	fmt.Println("type=", context)
	var role param.RoleQuery
	var err error
	if context == "application/json" {
		err = ctx.BindJSON(&role)
	} else {
		err = ctx.BindQuery(&role)
	}
	if err != nil {
		fmt.Println("err", err)
	}
	fmt.Println(role)
	ctx.JSON(http.StatusOK, model.JSONResult{
		Code: 10000,
		Msg:  "成功",
	})
}
